package com.atguigu.zqkJdbc;

import com.atguigu.bean.User;
import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Date:2020/8/28
 * Author:ybc
 * Description:
 */
public class ZqkMysqlData2BeanTest {
    /*
        j2ee,j2se,j2me
        dom4j,log4j
        i18n
     */
    @Test
    public void testGetBeanList() {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> list = new ArrayList<>();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            ps = connection.prepareStatement("select  * from t_user");
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getInt(5));
                list.add(user);
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        for (User user : list) {
            System.out.println(user);

        }

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhang_s", "root", "123456");
            ps = connection.prepareStatement("select * from t_user where username = ? and password=?");
            rs = ps.executeQuery();

            if (rs.next()) {
                System.out.println("登录成功");
                User user = new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getInt(5));
                System.out.println(user);
            } else {
                System.out.println("登录失败");
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
